package com.bcbim.api_web.pipeManagement.controller;


import com.alibaba.fastjson.JSONArray;
import com.bcbim.api_web.sys.interceptor.TranslateHandle;
import com.bcbim.api_web.sys.redis.MyRedisService;
import com.bcbim.authentication_interface.util.AuthUtil;
import com.bcbim.common.response.vo.ReturnMsg;
import com.bcbim.common.sys.Constant;
import com.bcbim.smart_water_interface.pipeManagement.vo.PipeListVO;
import com.bcbim.smart_water_interface.pipeManagement.vo.PipePictureVO;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 管网管理展示图控制层
 *
 * @author wxc
 * @version V 1.0.0
 * @date 2021/8/27
 **/
@Slf4j
@RestController
@RequestMapping("/pipePicture")
public class PipeManagementPictureController {
    @Autowired
    private MyRedisService myRedisService;

    /**
     * 管点类型 统计分析
     *
     * @return com.bcbim.common.response.vo.ReturnMsg
     * @author wxc
     * @date 2021/8/27  13:26
     */
    @PostMapping("/getPipelinePoint")
    public ReturnMsg getPipelinePoint() {
        //初始化集合
        List<PipePictureVO> list = Lists.newArrayList();
        // 获取redis中的数据
        String pointToken = AuthUtil.getPointToken();
        JSONArray jsonArray = (JSONArray) myRedisService.get(pointToken);
        // 如果不存在数据返回空
        if (ObjectUtils.isEmpty(jsonArray)) {
            log.error("管点类型统计异常");
            return ReturnMsg.consSuccess("管点类型统计异常");
        }
        // 翻译redis中集合
        List<PipeListVO> pointList = jsonArray.toJavaList(PipeListVO.class);
        TranslateHandle.translationData(pointList);
//        对redis中的数据进行统计分析
        // 使用流操作对集合分组获得map
        Map<String, Long> collect = pointList.stream().filter(obj -> ObjectUtils.isNotEmpty(obj.getUses())).collect(Collectors.groupingBy(PipeListVO::getUses, Collectors.counting()));
        // 遍历map，获得分组的数量
        Set<Map.Entry<String, Long>> entries = collect.entrySet();
        for (Map.Entry<String, Long> entry : entries) {
            PipePictureVO code = new PipePictureVO();
            code.setName(entry.getKey());
            code.setValue(entry.getValue().toString());
            list.add(code);
        }
        return ReturnMsg.consSuccess(list);
    }

    /**
     * 管线类型 统计分析
     *
     * @return com.bcbim.common.response.vo.ReturnMsg
     * @author wxc
     * @date 2021/8/27  13:34
     */
    @PostMapping("/getPipelineLine")
    public ReturnMsg getPipelineLine() {
        String lineToken = AuthUtil.getLineToken();
        List<PipePictureVO> list = Lists.newArrayList();
        // 获取redis中的管线集合
        JSONArray jsonArray = (JSONArray) myRedisService.get(lineToken);
        if (ObjectUtils.isEmpty(jsonArray)) {
            log.error("管线类型统计异常");
            return ReturnMsg.consSuccess("管线类型统计异常");
        }
        // 翻译分组
        List<PipeListVO> lineList = jsonArray.toJavaList(PipeListVO.class);
        TranslateHandle.translationData(lineList);
        Map<String, Long> collect = lineList.stream().filter(obj -> ObjectUtils.isNotEmpty(obj.getUses())).collect(Collectors.groupingBy(PipeListVO::getUses, Collectors.counting()));
        // 遍历map，获得分组的数量
        Set<Map.Entry<String, Long>> entries = collect.entrySet();
        for (Map.Entry<String, Long> entry : entries) {
            PipePictureVO code = new PipePictureVO();
            code.setName(entry.getKey());
            code.setValue(entry.getValue().toString());
            list.add(code);
        }
        //返回前端数据
        return ReturnMsg.consSuccess(list);
    }

    /**
     * 管线材质分析
     *
     * @return com.bcbim.common.response.vo.ReturnMsg
     * @author wxc
     * @date 2021/8/27  13:53
     */
    @PostMapping("/getPipelineLineMaterial")
    public ReturnMsg getPipelineLineMaterial() {
        String lineToken = AuthUtil.getLineToken();
        List<PipePictureVO> list = Lists.newArrayList();
        // 获取redis中的全部管线数据
        JSONArray jsonArray = (JSONArray) myRedisService.get(lineToken);
        if (ObjectUtils.isEmpty(jsonArray)) {
            log.error("管线材质统计异常");
            return ReturnMsg.consSuccess("管线材质统计异常");
        }
        // 翻译分组
        List<PipeListVO> lineList = jsonArray.toJavaList(PipeListVO.class);
        TranslateHandle.translationData(lineList);
        //传递给前端
        Map<String, Long> collect = lineList.stream().filter(obj -> ObjectUtils.isNotEmpty(obj.getMaterial())).collect(Collectors.groupingBy(PipeListVO::getMaterial, Collectors.counting()));
        // 遍历map，获得分组的数量
        Set<Map.Entry<String, Long>> entries = collect.entrySet();
        for (Map.Entry<String, Long> entry : entries) {
            PipePictureVO code = new PipePictureVO();
            code.setName(entry.getKey());
            code.setValue(entry.getValue().toString());
            list.add(code);
        }
        //返回前端数据
        return ReturnMsg.consSuccess(list);
    }

    /**
     * 管线管径统计分析
     *
     * @return com.bcbim.common.response.vo.ReturnMsg
     * @author wxc
     * @date 2021/8/27  14:34
     */
    @PostMapping("/getPipelineLineDiameter")
    public ReturnMsg getPipelineLineDiameter() {
        String lineToken = AuthUtil.getLineToken();
        List<PipePictureVO> list = Lists.newArrayList();
        // 获取redis中的全部管线数据
        JSONArray jsonArray = (JSONArray) myRedisService.get(lineToken);
        if (ObjectUtils.isEmpty(jsonArray)) {
            log.error("管线管径统计异常");
            return ReturnMsg.consSuccess("管线管径统计异常");
        }
        // 翻译分组
        List<PipeListVO> lineList = jsonArray.toJavaList(PipeListVO.class);
        TranslateHandle.translationData(lineList);
        Map<String, Long> collect = lineList.stream().filter(obj -> ObjectUtils.isNotEmpty(obj.getPipeDiameter())).collect(Collectors.groupingBy(item -> {
            Integer num = item.getPipeDiameter();
            String str = "其他";
            if (num >= Constant.PIPE_DIAMETER_TYPE_1 && num < Constant.PIPE_DIAMETER_TYPE_5) {
                str = Constant.PIPE_DIAMETER_TYPE_1 + "-" + Constant.PIPE_DIAMETER_TYPE_5;
            } else if (num >= Constant.PIPE_DIAMETER_TYPE_5 && num < Constant.PIPE_DIAMETER_TYPE_9) {
                str = Constant.PIPE_DIAMETER_TYPE_5 + "-" + Constant.PIPE_DIAMETER_TYPE_9;
            } else if (num >= Constant.PIPE_DIAMETER_TYPE_9 && num < Constant.PIPE_DIAMETER_TYPE_13) {
                str = Constant.PIPE_DIAMETER_TYPE_9 + "-" + Constant.PIPE_DIAMETER_TYPE_13;
            } else if (num >= Constant.PIPE_DIAMETER_TYPE_13 && num < Constant.PIPE_DIAMETER_TYPE_17) {
                str = Constant.PIPE_DIAMETER_TYPE_13 + "-" + Constant.PIPE_DIAMETER_TYPE_17;
            } else if (num >= Constant.PIPE_DIAMETER_TYPE_17 && num < Constant.PIPE_DIAMETER_TYPE_21) {
                str = Constant.PIPE_DIAMETER_TYPE_17 + "-" + Constant.PIPE_DIAMETER_TYPE_21;
            } else if (num >= Constant.PIPE_DIAMETER_TYPE_21) {
                str = ">=" + Constant.PIPE_DIAMETER_TYPE_21;
            }
            return str;
        }, Collectors.counting()));
        // 遍历map，获得分组的数量
        Set<Map.Entry<String, Long>> entries = collect.entrySet();
        for (Map.Entry<String, Long> entry : entries) {
            PipePictureVO code = new PipePictureVO();
            code.setName(entry.getKey());
            code.setValue(entry.getValue().toString());
            list.add(code);
        }
        //返回前端数据
        return ReturnMsg.consSuccess(list);
    }

    /**
     * 管点附属物统计分析
     *
     * @return com.bcbim.common.response.vo.ReturnMsg
     * @author wxc
     * @date 2021/8/27  14:47
     */
    @PostMapping("/getPipelinePointAttachment")
    public ReturnMsg getPipelinePointAttachment() {
        //初始化集合
        List<PipePictureVO> list = Lists.newArrayList();
        // 获取redis中的数据
        String pointToken = AuthUtil.getPointToken();
        JSONArray jsonArray = (JSONArray) myRedisService.get(pointToken);
        // 如果不存在数据返回空
        if (ObjectUtils.isEmpty(jsonArray)) {
            log.error("管点附属物统计异常");
            return ReturnMsg.consSuccess("管点附属物统计异常");
        }
        // 翻译redis中集合
        List<PipeListVO> pointList = jsonArray.toJavaList(PipeListVO.class);
        TranslateHandle.translationData(pointList);
//        对redis中的数据进行统计分析
        // 使用流操作对集合分组获得map
        Map<String, Long> collect = pointList.stream().filter(obj -> ObjectUtils.isNotEmpty(obj.getAttachment())).collect(Collectors.groupingBy(PipeListVO::getAttachment, Collectors.counting()));
        // 遍历map，获得分组的数量
        Set<Map.Entry<String, Long>> entries = collect.entrySet();
        for (Map.Entry<String, Long> entry : entries) {
            PipePictureVO code = new PipePictureVO();
            code.setName(entry.getKey());
            code.setValue(entry.getValue().toString());
            list.add(code);
        }
        return ReturnMsg.consSuccess(list);
    }

    /**
     * 管点特征统计
     *
     * @return com.bcbim.common.response.vo.ReturnMsg
     * @author wxc
     * @date 2021/8/27  14:57
     */
    @PostMapping("/getPipelinePointFeature")
    public ReturnMsg getPipelinePointFeature() {
        //初始化集合
        List<PipePictureVO> list = Lists.newArrayList();
        // 获取redis中的数据
        String pointToken = AuthUtil.getPointToken();
        JSONArray jsonArray = (JSONArray) myRedisService.get(pointToken);
        // 如果不存在数据返回空
        if (ObjectUtils.isEmpty(jsonArray)) {
            log.error("管点特征统计异常");
            return ReturnMsg.consSuccess("管点特征统计异常");
        }
        // 翻译redis中集合
        List<PipeListVO> pointList = jsonArray.toJavaList(PipeListVO.class);
        TranslateHandle.translationData(pointList);
//        对redis中的数据进行统计分析
        // 使用流操作对集合分组获得map
        Map<String, Long> collect = pointList.stream().filter(obj -> ObjectUtils.isNotEmpty(obj.getFeature())).collect(Collectors.groupingBy(PipeListVO::getFeature, Collectors.counting()));
        // 遍历map，获得分组的数量
        Set<Map.Entry<String, Long>> entries = collect.entrySet();
        for (Map.Entry<String, Long> entry : entries) {
            PipePictureVO code = new PipePictureVO();
            code.setName(entry.getKey());
            code.setValue(entry.getValue().toString());
            list.add(code);
        }
        return ReturnMsg.consSuccess(list);
    }

    /**
     * 井底埋深统计
     *
     * @return com.bcbim.common.response.vo.ReturnMsg
     * @author wxc
     * @date 2021/8/27  17:55
     */
    @PostMapping("/getPipelinePointBury")
    public ReturnMsg getPipelinePointBury() {
        List<PipePictureVO> list = Lists.newArrayList();
        // 获取redis中的数据
        String pointToken = AuthUtil.getPointToken();
        JSONArray jsonArray = (JSONArray) myRedisService.get(pointToken);
        // 如果不存在数据返回20
        if (ObjectUtils.isEmpty(jsonArray)) {
            log.error("管点井底埋深统计异常");
            return ReturnMsg.consSuccess("管点井底埋深统计异常");
        }
        List<PipeListVO> pointList = jsonArray.toJavaList(PipeListVO.class);
        // 过滤空数据
        List<PipeListVO> collect = pointList.stream().filter(obj -> ObjectUtils.isNotEmpty(obj.getBury())).collect(Collectors.toList());
        //创建map，存放发返回前端的值
        Map<String, Long> map = new LinkedHashMap<>(9);
        map.put(Constant.PIPE_BURY_TYPE_0 + "-" + Constant.PIPE_BURY_TYPE_1, 0L);
        map.put(Constant.PIPE_BURY_TYPE_1 + "-" + Constant.PIPE_BURY_TYPE_2, 0L);
        map.put(Constant.PIPE_BURY_TYPE_2 + "-" + Constant.PIPE_BURY_TYPE_3, 0L);
        map.put(Constant.PIPE_BURY_TYPE_3 + "-" + Constant.PIPE_BURY_TYPE_4, 0L);
        map.put(Constant.PIPE_BURY_TYPE_4 + "-" + Constant.PIPE_BURY_TYPE_5, 0L);
        map.put(Constant.PIPE_BURY_TYPE_5 + "-" + Constant.PIPE_BURY_TYPE_6, 0L);
        map.put(Constant.PIPE_BURY_TYPE_6 + "-" + Constant.PIPE_BURY_TYPE_7, 0L);
        map.put(Constant.PIPE_BURY_TYPE_7 + "-" + Constant.PIPE_BURY_TYPE_8, 0L);
        map.put(Constant.PIPE_BURY_TYPE_8 + "-" + Constant.PIPE_BURY_TYPE_9, 0L);
        map.put(Constant.PIPE_BURY_TYPE_9 + "以上", 0L);

        // 遍历集合值，将值存入map中
        for (PipeListVO item : collect) {
            String key;
            int i = (int) Math.floor(Double.parseDouble(item.getBury()));
            if (i >= Constant.PIPE_BURY_TYPE_9) {
                key = Constant.PIPE_BURY_TYPE_9 + "以上";
            } else {
                key = i + "-" + (i + 1);
            }
            map.replace(key, map.get(key) + 1);
        }
        // 遍历map，获得分组的数量
        Set<Map.Entry<String, Long>> entries = map.entrySet();
        for (Map.Entry<String, Long> entry : entries) {
            PipePictureVO code = new PipePictureVO();
            code.setName(entry.getKey());
            code.setValue(entry.getValue().toString());
            list.add(code);
        }
        return ReturnMsg.consSuccess(list);
    }

    /**
     * 地面高程统计
     *
     * @return com.bcbim.common.response.vo.ReturnMsg
     * @author wxc
     * @date 2021/8/30  8:36
     */
    @PostMapping("/getPipelinePointHigh")
    public ReturnMsg getPipelinePointHigh() {
        List<PipePictureVO> list = Lists.newArrayList();
        // 获取redis中的数据
        String pointToken = AuthUtil.getPointToken();
        JSONArray jsonArray = (JSONArray) myRedisService.get(pointToken);
        // 如果不存在数据返回20
        if (ObjectUtils.isEmpty(jsonArray)) {
            log.error("管点地面高程统计异常");
            return ReturnMsg.consSuccess("管点地面高程统计异常");
        }
        List<PipeListVO> pointList = jsonArray.toJavaList(PipeListVO.class);
        // 过滤空数据
        List<PipeListVO> collect = pointList.stream().filter(obj -> ObjectUtils.isNotEmpty(obj.getHigh())).collect(Collectors.toList());
        // 遍历元素将埋深管点转化为小数类型
        Map<String, Long> map = new LinkedHashMap<>(9);

        map.put(Constant.PIPE_HIGH_TYPE_5 + "以下", 0L);
        map.put(Constant.PIPE_HIGH_TYPE_5 + "~" + Constant.PIPE_HIGH_TYPE_4, 0L);
        map.put(Constant.PIPE_HIGH_TYPE_4 + "~" + Constant.PIPE_HIGH_TYPE_3, 0L);
        map.put(Constant.PIPE_HIGH_TYPE_3 + "~" + Constant.PIPE_HIGH_TYPE_2, 0L);
        map.put(Constant.PIPE_HIGH_TYPE_2 + "~" + Constant.PIPE_HIGH_TYPE_1, 0L);
        map.put(Constant.PIPE_HIGH_TYPE_1 + "~" + Constant.PIPE_HIGH_TYPE_0, 0L);
        map.put(Constant.PIPE_HIGH_TYPE_0 + "以上", 0L);

        for (PipeListVO item : collect) {
            String key;
            int i = (int) Math.ceil(Double.parseDouble(item.getHigh()));
            if (i <= Constant.PIPE_HIGH_TYPE_5) {
                key = Constant.PIPE_HIGH_TYPE_5 + "以下";
            } else if (i >= Constant.PIPE_HIGH_TYPE_0) {
                key = Constant.PIPE_HIGH_TYPE_0 + "以上";
            } else {
                key = (i - 1) + "~" + i;
            }
            map.replace(key, map.get(key) + 1);
        }

        // 遍历map，获得分组的数量
        Set<Map.Entry<String, Long>> entries = map.entrySet();
        for (Map.Entry<String, Long> entry : entries) {
            PipePictureVO code = new PipePictureVO();
            code.setName(entry.getKey());
            code.setValue(entry.getValue().toString());
            list.add(code);
        }
        return ReturnMsg.consSuccess(list);
    }
}
